export default function createRouteMap(routes, oldPathMap) {
  // pathMap 扁平化
  let pathMap = oldPathMap || Object.create(null);
  routes.forEach(route => {
    addRouteRecord(route, pathMap, null)
  })
  console.log(pathMap)
  return {
    pathMap
  }
};

function addRouteRecord(route, pathMap, parent) {
  const { path, component, name, children } = route;
  const allPath = parent ? parent.path + '/' + path : path;
  let record = {
    path: allPath,
    component,
    name,
  }
  pathMap[allPath] = record;
  // 递归
  if (children) {
    children.forEach(route => {
      addRouteRecord(route, pathMap, record)
    })
  }
}